import {Component, OnInit} from '@angular/core';
import {App, NavController} from 'ionic-angular';
import {ModifyPswPage} from '../modifyPsw/modifyPsw';
import {ChildPage} from "../child/child";
import {UserLogin} from "../../modules/app-user/user-login/user-login";
import {UserService} from "../../providers/user-service";
import {AppPopup} from "../../util/popup";
import {UserDetail} from "../../modules/app-user/user-detail/user-detail";

@Component({
    selector: 'page-person',
    templateUrl: 'person.html'
})
export class PersonPage implements OnInit {
    personalDetail = {
        "companyAddressId": "",
        "companyAddressName": "",
        "companyId": "",
        "companyName": "",
        "id": "",
        "mobile": "",
        "name": "",
        "type": null,
        "companyMemberLimit": "",
        "companyMobile": "",
    };

    constructor(public navCtrl: NavController,
                private userServ: UserService,
                private popup: AppPopup,
                private app: App,) {
    }

    ngOnInit() {
    }

    ionViewDidEnter() {
        this.userServ.getPersonInfo()
            .then(res => {
                console.info("用户详情获取成功：",res);
                this.personalDetail = res.infoData;
            })
            .catch(err => {
                console.error("用户详情获取失败：",err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        })
                } else {
                    this.popup.alert(err.errorMassage);
                }

            });
    }

    goChild() {
        this.navCtrl.push(ChildPage);
    }

    goPersonalData() {
        this.navCtrl.push(UserDetail);
    }

    goModifyPsw() {
        this.navCtrl.push(ModifyPswPage);
    }

    //退出登录
    logout() {
        this.popup.confirm("你确定要退出登录吗？")
            .then(() => {
                this.userServ.logout()
                    .then(() => {
                        console.log("退出登录");
                        localStorage.removeItem("user");
                        this.app.getRootNav().setRoot(UserLogin);
                    })
                    .catch(err => {
                        console.log("loginOut,登出失败：", err);
                        if (err.errorMassage == "未登录或登录超时！") {
                            this.popup.alert(err.errorMassage)
                                .then(() => {
                                    this.app.getRootNav().setRoot(UserLogin);
                                })
                        } else {
                            this.popup.alert(err.errorMsg);
                        }
                    });
            })
            .catch(err => {
                console.error(err);
            })
    }
}